package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.twoHourSummary_report.TwoHourSummaryReportDataBean;
import com.navi.bean.out.twoHourSummary_report.TwoHourSummaryReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.reportUtils.LKingSortUtil;
import com.navi.mould1.service.ITwoHourSummaryReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.sqlServer_QueryRunner_QueryListBySql;
import static com.navi.mould1.reportUtils.ComputeUtil.getAverageRate;


@Slf4j
@Service
public class TwoHourSummaryReportServiceImpl implements ITwoHourSummaryReportService {


    public List<TwoHourSummaryReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("*");
        sqlVo.appendSql("FROM (");
            sqlVo.appendSql("SELECT");
            sqlVo.appendSql("ROW_NUMBER() OVER(PARTITION BY lot_number ORDER BY bom_material_code) as row_number,");
            sqlVo.appendSql("*");
            sqlVo.appendSql("FROM dbo.iv_distribution");
            sqlVo.appendSql("WHERE 1=1");
            if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND equipment_code in" + jointInSql(conditions.getBay_id()));
            if (StringUtils.isNotEmpty(conditions.getComponent_cell_efficiency())) sqlVo.appendSql(Arrays.asList(conditions.getComponent_cell_efficiency().split(",")).stream().map(data -> "attr_1 like '%" + data + "%'").collect(Collectors.joining(" OR ", " AND (", ")")));
            if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND test_time >= ?", conditions.getStart_time());
            if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND test_time < ?", conditions.getEnd_time());
        sqlVo.appendSql(") AS tbl");
        sqlVo.appendSql("WHERE tbl.row_number = 1;");

        List<TwoHourSummaryReportDataBean> resultDatas = sqlServer_QueryRunner_QueryListBySql(sqlVo, TwoHourSummaryReportDataBean.class);

        List<TwoHourSummaryReportGridOut> gridOut_list = new ArrayList<>();
        resultDatas.stream().collect(Collectors.groupingBy(data ->  timestampTo2Hour(data.getTest_time()) + data.getEquipment_code() + data.getAttr_1())).forEach((key,list) -> {
            TwoHourSummaryReportGridOut gridOut = new TwoHourSummaryReportGridOut();
            gridOut.setDate(timestampTo2Hour(list.get(0).getTest_time()).substring(0, 10));
            gridOut.setHour(timestampTo2Hour(list.get(0).getTest_time()).substring(11, 13));
            gridOut.setLine_id(list.get(0).getEquipment_code());
            gridOut.setAttr_1(list.get(0).getAttr_1());
            gridOut.setCount(list.size());
            gridOut.setCtm(String.valueOf(getAverageRate(list.stream().mapToDouble(data -> Double.parseDouble(data.getDec_ctm())).sum() * 100, list.size(), 2)) + "%");
            gridOut.setPm(getAverageRate(list.stream().mapToDouble(data -> Double.parseDouble(data.getPm())).sum(), list.size(), 2));
            gridOut.setVoc(getAverageRate(list.stream().mapToDouble(data -> Double.parseDouble(data.getVoc())).sum(), list.size(), 2));
            gridOut.setIsc(getAverageRate(list.stream().mapToDouble(data -> Double.parseDouble(data.getIsc())).sum(), list.size(), 3));
            gridOut.setFf(getAverageRate(list.stream().mapToDouble(data -> Double.parseDouble(data.getFf())).sum(), list.size(), 3));
            gridOut.setRs(getAverageRate(list.stream().mapToDouble(data -> Double.parseDouble(data.getRs())).sum(), list.size(), 3));
            gridOut.setRsh(getAverageRate(list.stream().mapToDouble(data -> Double.parseDouble(data.getRsh())).sum(), list.size(), 0));
            gridOut_list.add(gridOut);
        });

        LKingSortUtil.sort(gridOut_list, new String[] { "date", "hour", "line_id", "attr_1" }, new boolean[] { true, true, true, true });

        /*** 增加一条汇总的数据 **/
        if (CollectionUtils.isNotEmpty(gridOut_list) && gridOut_list != null) {
            TwoHourSummaryReportGridOut gridOut = new TwoHourSummaryReportGridOut();
            gridOut.setDate("总计");
            gridOut.setCount(resultDatas.size());
            gridOut.setCtm(String.valueOf(getAverageRate(resultDatas.stream().mapToDouble(data -> Double.parseDouble(data.getDec_ctm())).sum() * 100, resultDatas.size(), 2)) + "%");
            gridOut.setPm(getAverageRate(resultDatas.stream().mapToDouble(data -> Double.parseDouble(data.getPm())).sum(), resultDatas.size(), 2));
            gridOut.setVoc(getAverageRate(resultDatas.stream().mapToDouble(data -> Double.parseDouble(data.getVoc())).sum(), resultDatas.size(), 2));
            gridOut.setIsc(getAverageRate(resultDatas.stream().mapToDouble(data -> Double.parseDouble(data.getIsc())).sum(), resultDatas.size(), 3));
            gridOut.setFf(getAverageRate(resultDatas.stream().mapToDouble(data -> Double.parseDouble(data.getFf())).sum(), resultDatas.size(), 3));
            gridOut.setRs(getAverageRate(resultDatas.stream().mapToDouble(data -> Double.parseDouble(data.getRs())).sum(), resultDatas.size(), 3));
            gridOut.setRsh(getAverageRate(resultDatas.stream().mapToDouble(data -> Double.parseDouble(data.getRsh())).sum(), resultDatas.size(), 0));
            gridOut_list.add(gridOut);
        }

        return gridOut_list;
    }









    /*** 以两小时汇分割 **/
    private String timestampTo2Hour(Timestamp timestamp) {
        String date = timestamp.toString().substring(0, 10);
        String hour = timestamp.toString().substring(11, 13);
        if (Integer.parseInt(hour) % 2 == 1) {
            hour = String.valueOf(Integer.parseInt(hour) - 1);
        }
        if (hour.length() <2) hour = "0" + hour;
        return date + " " + hour;
    }

}
